<?php
require_once (__DIR__ . "/../inc/functions.php");
require_once (__DIR__ . "/Keywords.php");

class contactMethods
{
	static function GetAll()
	{
		$conn = GetConnection();
		$results = $conn->query('SELECT * FROM contactMethods');
		$error = $conn->error;
		return $results; 
	}
	
	static function Lookup($name,$value)
	{
		$results=contactMethods::GetAll();
		while($row = $results->fetch_assoc())
		{
			// Check that contact type is right
			$contactType = Keywords::GetName($row['contactType']);
			if ($name == $contactType
				// check that value matches 
				&& $value == $row['value'])
				 	// we have a match 
				 	return $row;
		}
		// no match found so return error			
		return Array('Server Error' => 'No match found');  
	}
	
	static function Get($id)
	{
		$conn = GetConnection();
		$results = $conn->query("SELECT * FROM contactMethods WHERE id=$id");
		$row = $results->fetch_assoc();
		$error = $conn->error;
		return $row;
	}
	
	static function Insert($row)
	{
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = 	"INSERT INTO contactMethods (contactType,userId,value) "
			.	"Values ($row2[contactType],$row2[userId],'$row2[value]'); ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Update($row)
	{
		//print_r ($row);	
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = 	"UPDATE contactMethods "
			.	"Set contactType=$row2[contactType],userId=$row2[userId],value='$row2[value]' "
			.	"WHERE id=$row2[id] ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;		
	}
	
	static function Delete($id)
	{
		$conn = GetConnection();
		$sql = 	"DELETE FROM contactMethods "
			.	"WHERE id=$id ";
		//echo $sql;
		$conn->query($sql);
		$error = $conn->error;
		$conn->close();
		
		return $error != '' ? array('Server Error' => $error) : true;				
	}
	static function Validate($row)
	{
		$results = array();
		if(empty($row['value'])) $results['value'] = 'Value is required';		
		return count($results) > 0 ? $results : true;
	}
}

?>
